using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;

namespace MyRobot
{
    public class GUI
    {
        private delegate void SetTextCallback(Form form, System.Windows.Forms.Control control, string text);
        private delegate void ClearListCallback(System.Windows.Forms.ListView control);
        private delegate ListViewItem AddListItemCallback(System.Windows.Forms.ListView control, string itemText);
        private delegate void AddListSubitemCallback(Form form, ListViewItem item, string subItemText);
        private delegate void SetImageCallback(Form form, System.Windows.Forms.PictureBox control, Bitmap image);

        // A method to echo debugging message to the console and also the DebugMessages textbox on the main form.
        public void DebugMessage(Form form, System.Windows.Forms.Control control, string message)
        {
            SetText(form, control, message);
            System.Diagnostics.Debug.WriteLine(message);
        }

        // Thread safe updating of control's text property.
        public void SetText(Form form, System.Windows.Forms.Control control, string text)
        {
            if (control.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                control.Invoke(d, new object[] { form, control, text });
            }
            else
            {
                if (control is TextBox)
                {                  
                    if (control.Text.Length == 0) control.Text += text;
                    else
                    {
                        control.Text += System.Environment.NewLine + text;
                        ((TextBox)control).SelectionStart = ((TextBox)control).Text.Length;
                        ((TextBox)control).ScrollToCaret();
                        ((TextBox)control).Refresh();
                    }
                }
                else control.Text = text;
            }
        }

        // Thread safe clearing of list view
        public void ClearList(System.Windows.Forms.ListView control)
        {
            if (control.InvokeRequired)
            {
                ClearListCallback d = new ClearListCallback(ClearList);
                control.Invoke(d, new object[] { control });
            }
            else
            {
                control.Items.Clear();
            }
        }

        // Thread safe adding of item to list control
        public ListViewItem AddListItem(System.Windows.Forms.ListView control, string itemText)
        {
            ListViewItem item = null;

            if (control.InvokeRequired)
            {
                AddListItemCallback d = new AddListItemCallback(AddListItem);
                item = (ListViewItem)control.Invoke(d, new object[] { control, itemText });
            }
            else
            {
                item = control.Items.Add(itemText);
            }

            return item;
        }

        // Thread safe adding of subitem to list control
        public void AddListSubitem(Form form, ListViewItem item, string subItemText)
        {
            if (form.InvokeRequired)
            {
                AddListSubitemCallback d = new AddListSubitemCallback(AddListSubitem);
                form.Invoke(d, new object[] { form, item, subItemText });
            }
            else
            {
                item.SubItems.Add(subItemText);
            }
        }

        // Thread safe updating of control's text property.
        public void SetImage(Form form, PictureBox pictureBox, Bitmap image)
        {
            if (pictureBox.InvokeRequired)
            {
                SetImageCallback d = new SetImageCallback(SetImage);
                pictureBox.Invoke(d, new object[] { form, pictureBox, image });
            }
            else
            {
                pictureBox.Image = image;
                //pictureBox.Refresh();
            }
        }
    }
}
